package cn.itcast.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import cn.itcast.pojo.User;
import cn.itcast.pojo.UserVO;

@Controller
@RequestMapping("hello")//相当于多一个二级路径
public class Hello2Controller {
	/**
	 * 映射url请求和方法
	 * @return
	 */
	@RequestMapping("show1")
	public ModelAndView test1(){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hello");
		mv.addObject("msg", "这是第一个springmvc的注解程序！");
		return mv;
	}
	
	/**
	 * ant风格的映射：
	 * 	1、？：表示匹配任意单个字符
	 * 	2、*：表示匹配任意多个字符，0个、1个、多个
	 * 	3、**：表示任意多级路径
	 */
	@RequestMapping("*/show2")
	public ModelAndView test2(){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hello");
		mv.addObject("msg", "这是ant风格的映射方式");
		return mv;
	}
	
	@RequestMapping("**/show3")
	public ModelAndView test3(){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hello");
		mv.addObject("msg", "这是ant风格的映射方式:**，表示任意多级路径");
		return mv;
	}
	@RequestMapping("show4/{name}/{id}")
	public ModelAndView test4(@PathVariable("name")String name,@PathVariable("id")Long id){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hello");
		mv.addObject("msg", "占位符的映射方式:"+name+"..."+id);
		return mv;
	}
	/**
	 * method=RequestMethod.POST:只有post请求能映射到这个方法
	 * @param name
	 * @param id
	 * @return
	 */
	@RequestMapping(value="show5",method=RequestMethod.POST)
	public ModelAndView test5(){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hello");
		mv.addObject("msg", "限定请求方法的映射：Post");
		return mv;
	}
	@RequestMapping(value="show6",method={RequestMethod.POST,RequestMethod.GET})
	public ModelAndView test6(){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hello");
		mv.addObject("msg", "限定请求方法的映射：Post和Get");
		return mv;
	}
	@RequestMapping(value="show7",params="userId")
	public ModelAndView test7(){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hello");
		mv.addObject("msg", "限定请求参数的映射：参数必须包含userId");
		return mv;
	}
	@RequestMapping(value="show8",params="!userId")
	public ModelAndView test8(){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hello");
		mv.addObject("msg", "限定请求参数的映射：参数必须不包含userId");
		return mv;
	}
	@RequestMapping(value="show9",params="userId=101")
	public ModelAndView test9(){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hello");
		mv.addObject("msg", "限定请求参数的映射：参数必须为userId=101");
		return mv;
	}
	@RequestMapping(value="show10",params="userId!=101")
	public ModelAndView test10(){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hello");
		mv.addObject("msg", "限定请求参数的映射：参数必须为userId!=101");
		return mv;
	}
	@RequestMapping(value="show11",params={"name","userId"})
	public ModelAndView test11(){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hello");
		mv.addObject("msg", "限定请求参数的映射：参数必须包含name和userId");
		return mv;
	}
	@RequestMapping(value="show12",params={"name=zhangsan","userId!=101"})
	public ModelAndView test12(){
		ModelAndView mv = new ModelAndView("hello");//在构造函数中设置视图名称
		mv.addObject("msg", "限定请求参数的映射：参数必须username为张三并且userId不为101");
		return mv;
	}
	
	//@RequestMapping(value="show13",method=RequestMethod.GET)
	@GetMapping("show13")
	public ModelAndView test13(){
		
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hello");
		mv.addObject("msg", "组合注解：get");
		return mv;
	}
	@PostMapping("show14")
	public ModelAndView test14(){
		
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hello");
		mv.addObject("msg", "组合注解：post");
		return mv;
	}
	@PutMapping("show15")
	public ModelAndView test15(){
		
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hello");
		mv.addObject("msg", "组合注解：put");
		return mv;
	}
	@DeleteMapping("show16")
	public ModelAndView test16(){
		
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hello");
		mv.addObject("msg", "组合注解：delete");
		return mv;
	}
	
	/**
	 * springmvc可以将String类型的返回值作为视图名称
	 * @return
	 */
	@RequestMapping("show17")
	public String test17(Model model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		
		model.addAttribute("msg", request+"</br>"+response+"</br>"+session+"</br>");
		
		return "hello";
	}
	
	@RequestMapping("show18/{name}/{id}")
	public String test18(Model model,@PathVariable("name")String name,@PathVariable("id")Long id){
		
		model.addAttribute("msg", name+"..."+id);
		
		return "hello";
	}
	
	@RequestMapping("show19")
	public String test19(Model model,@RequestParam(value="name",required=false)String name){
		
		model.addAttribute("msg", name);
		
		return "hello";
	}
	
	
	@RequestMapping("show20")
	public String test20(Model model,@RequestParam(value="name", required=true,defaultValue="lisi")String name){
		
		model.addAttribute("msg", name);
		
		return "hello";
	}
	@RequestMapping("show21")
	public String test21(Model model,HttpServletRequest request){
		
		//获取cookie
		Cookie[] cookies = request.getCookies();
		if (cookies!=null) {
			
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals("JSESSIONID")) {
					model.addAttribute("msg", cookie.getValue());
				}
			}
		}
		
		return "hello";
	}
	
	
	@RequestMapping("show22")
	public String test22(Model model,@CookieValue(value="JSESSIONID",required=false)String jsessionid){
		
		model.addAttribute("msg", jsessionid);
		
		
		return "hello";
	}
	@RequestMapping("show23")
	@ResponseStatus(value=HttpStatus.OK)//响应状态码
	public void test23(@RequestParam("name")String name,
			@RequestParam("age")Integer age,
			@RequestParam("isMarry")Boolean isMarry,
			@RequestParam("income")Float income,
			@RequestParam("interests")String[] interests){
		
		
		StringBuffer sb = new StringBuffer();
		sb.append("name:"+name+"\n");
		sb.append("age:"+age+"\n");
		sb.append("isMarry:"+isMarry+"\n");
		sb.append("income:"+income+"\n");
		sb.append("[");
		for (String str : interests) {
			sb.append(str+" ");
		}
		sb.append("]");
		
		System.out.println(sb.toString());
		
		
	}

	@RequestMapping("show24")
	public String test24(Model model,User user,@RequestParam("name")String name){
		model.addAttribute("msg", user+"..."+name);
		return "hello";
	}
	@RequestMapping("show25")
	public String test25(Model model,UserVO userVO){
		model.addAttribute("msg", userVO);
		return "hello";
	}
	
	@RequestMapping("show26")
	public String test26(Model model,@RequestParam("ids")List<Long> ids){
		model.addAttribute("msg", ids);
		return "hello";
	}
	
	@RequestMapping("show27")
	public String test27(Model model){
		
		List<User> list = new ArrayList<User>();
		for (int i = 0; i < 20; i++) {
			User user = new User();
			user.setId(i+1l);
			user.setUsername("admin"+i);
			user.setName("用户"+i);
			user.setAge(18+i);
			list.add(user);
		}
		
		model.addAttribute("users", list);
		
		return "userList";
	}
	
	@RequestMapping("show28")
	@ResponseBody//可以将方法的返回值响应成一个json格式的数据
	public List<User> test28(){
		
		List<User> list = new ArrayList<User>();
		for (int i = 0; i < 20; i++) {
			User user = new User();
			user.setId(i+1l);
			user.setUsername("admin"+i);
			user.setName("用户"+i);
			user.setAge(18+i);
			list.add(user);
		}
		
		return list;
	}
	
	
	@RequestMapping("show29")
	public String test29(Model model,@RequestBody()User user){
		
		model.addAttribute("msg", user);
		
		return "hello";
	}
	@RequestMapping("show30")
	public String test30(Model model,@RequestBody()String user){
		
		model.addAttribute("msg", user);
		
		return "hello";
	}
	@RequestMapping("show31")
	public String test31(Model model,@RequestParam("file") MultipartFile file) throws Exception{
		
		if (file!=null) {
			file.transferTo(new File("d://upload//"+file.getOriginalFilename()));
		}
		
		model.addAttribute("msg", "上传成功！");
		return "hello";
	}
	@RequestMapping("show32")
	public String test32(){
		System.out.println("show32.do");
		
		return "forward:/hello/show34.do?type=forward";
	}
	@RequestMapping("show33")
	public String test33(){
		
		System.out.println("show33.do");
		
		return "redirect:show34.do?type=redirect";
	}
	
	@RequestMapping("show34")
	public String test34(Model model,@RequestParam("type")String type){
		
		model.addAttribute("msg", type);
		
		return "hello";
	}
	@RequestMapping("show35")
	public String test35(Model model){
		
		System.out.println("自定义处理器执行了");
		model.addAttribute("msg", "自定义处理器执行了");
		
		return "hello";
	}
	@RequestMapping("show36")
	public String test36(@RequestParam("name") String name) {
		System.out.println(name);
		return "hello";
	}

	
	
}
